package com.whuan.user.service.impl;


import com.whuan.user.dataObject.User;
import com.whuan.user.dto.UserDTO;
import com.whuan.user.enums.ResultEnum;
import com.whuan.user.exception.IbestException;
import com.whuan.user.form.UserForm;
import com.whuan.user.repository.UserRepository;
import com.whuan.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    @Override
    public User findByUserId(String userId) {
        User user = userRepository.findByUserId(userId);
        return user;
    }



    @Override
    public User findByUserForm(UserForm userForm){
        User user = userRepository.findByUserNameAndUserPassword(userForm.getName(), userForm.getPassword());
        return user;
    }

    /**
     * create by: whaun
     * create date:  2019/5/20 time:7:21
     * description: 用户注册，参数：userDTO
     */
    @Override
    public User save(UserDTO userDTO) {
        //1、如果UserDTO为空，抛出异常
        if(userDTO==null){
            throw new IbestException(ResultEnum.REGISTER_EXCEPTION);
        }
        //2、注册信息拷贝
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        //3、正式注册
        User user1 = userRepository.save(user);
        return user1;
    }
}
